package com.bstek.tool;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;
import org.apache.cordova.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by ilulu on 14-4-17.
 */
public class SimpleActivity extends CordovaActivity{
    String domain;
    List<String> urls=new ArrayList<String>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Config.init(this);
    }

    void initWebPage(){
        SharedPreferences demoInfo = getSharedPreferences("demoInfo", 0);
        String ip=demoInfo.getString("ip", "");
        String port=demoInfo.getString("port", "");
        String root=demoInfo.getString("root", "");
        String page=getIntent().getExtras().getString("path");

        StringBuffer url=new StringBuffer("http://");
        if(!ip.isEmpty()){
            url.append(ip);
        }
        if(!port.isEmpty()){
            url.append(":"+port);
        }
        if(!root.isEmpty()){
            url.append("/"+root);
        }
        if(!page.isEmpty()){
            url.append("/"+page);
        }

        domain="http://";
        if(!ip.isEmpty()){
            domain+=ip;
        }
        if(!port.isEmpty()){
            domain+=":"+port;
        }
        if(!root.isEmpty()){
            domain+="/"+root;
        }

        appView.loadUrl(url.toString(),100000);
        appView.setWebViewClient(new CordovaWebViewClient(this,appView){
            @Override
            public void onPageFinished(WebView view, String url) {
                String urlReal = url.split("#")[0];
                if(!urls.contains(urlReal)) {
                    String js = "javascript:var a = document.createElement(\"script\");\n" +
                            "            a.type = \"text/javascript\";\n" +
                            "            a.src = '"+domain+"/cordovaJS/platform/android/cordova.js';\n" +
                            "            var head = document.getElementsByTagName(\"head\")[0];\n" +
                            "            head.appendChild(a);";
                    view.loadUrl(js);
                    String js2 = "javascript:var ab = document.createElement(\"script\");\n" +
                            "            ab.type = \"text/javascript\";\n" +
                            "            ab.src = '"+domain+"/cordovaJS/bn.js';\n" +
                            "            var head = document.getElementsByTagName(\"head\")[0];\n" +
                            "            head.appendChild(ab);";
                    view.loadUrl(js2);
                    urls.add(urlReal);
                    super.onPageFinished(view, url);
                }
            }
        });
    }
}